package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.BomProductDao;
import com.xbongbong.saas.domain.entity.BomProductEntity;
import com.xbongbong.saas.model.BomProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/2/1 14:34
 * @since v1.0
 */
@Service("bomProductModel")
public class BomProductModelImpl implements BomProductModel {
    @Resource
    private BomProductDao bomProductDao;

    @Override
    public Integer insert(BomProductEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return bomProductDao.insert(entity);
    }

    @Override
    public Integer insertBatch(List<BomProductEntity> list) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        StandardizeDataUtil.standardize4BomProductEntityList(list);
        return bomProductDao.insertBatch(list);
    }

    @Override
    public Integer update(BomProductEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return bomProductDao.update(entity);
    }

    @Override
    public Integer save(BomProductEntity entity) throws XbbException {
        Long id = entity.getId();
        if (id == null || Objects.equals(id, 0L)) {
            return insert(entity);
        } else {
            return update(entity);
        }
    }

    @Override
    public List<BomProductEntity> findEntitys(Map<String, Object> param) {
        return bomProductDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return bomProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public BomProductEntity getByKey(Long key, String corpid) {
        return bomProductDao.getByKey(key, corpid);
    }

    @Override
    public List<BomProductEntity> getProductsByBomId(Long bomId, String corpid, Integer productType) {
        // 获取bom单关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("del", 0);
        params.put("bomId", bomId);
        params.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        if (Objects.nonNull(productType)) {
            params.put("type", productType);
        }
        return findEntitys(params);
    }

    @Override
    public List<BomProductEntity> getProductsByBomId(Long bomId, String corpid, Integer productType, List<Integer> delIn) {
        // 获取bom单关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("delIn", delIn);
        params.put("bomId", bomId);
        params.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        if (Objects.nonNull(productType)) {
            params.put("type", productType);
        }
        return findEntitys(params);
    }

    @Override
    public Integer deleteByBomIdIn(List<Long> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        return bomProductDao.deleteByBomIdIn(list, corpid);
    }

    @Override
    public Integer deleteByIdIn(List<Long> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        return bomProductDao.deleteByIdIn(list, corpid);
    }

    @Override
    public Integer dataConsistencyUpdate(String corpid, Long productId, String productName, String specification, String productNo) {
        return bomProductDao.dataConsistencyUpdate(corpid, productId, productName, specification, productNo);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer type, Integer isArray) {
        return bomProductDao.isProductSubAttrExist(attr, corpid, type, isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn,Integer type){
        return bomProductDao.summaryByAttr(field,corpid,  refId, productIdIn,type);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field, String corpid, Long refId, List<Long> productIdIn, Integer type) {
        return bomProductDao.summaryByAttrFromOutSide(field,corpid,  refId, productIdIn,type);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer type, Integer isArray) {
        return bomProductDao.countProductSub(attr, corpid, type, isArray);
    }

    @Override
    public List<BomProductEntity> getProductWithSubData(String attr, String corpid, Integer type, Integer isArray, Long idGte, Integer limit) {
        return bomProductDao.getProductWithSubData(attr, corpid, type, isArray, idGte, limit);
    }

    @Override
    public Integer updateBatch(List<BomProductEntity> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (BomProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4BomProductEntityList(list);
        return bomProductDao.updateBatch(list, corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> list, String corpid, Integer del) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        return bomProductDao.deleteBatch(list, corpid, del);
    }
}
